/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.notice.controller;

import com.fzhucc.annotation.AnonymousAccess;
import com.fzhucc.annotation.Log;
import com.fzhucc.annotation.rest.AnonymousGetMapping;
import com.fzhucc.modules.activity.domain.dto.ActivityDto;
import com.fzhucc.modules.notice.domain.Notice;
import com.fzhucc.modules.notice.domain.dto.NoticeAddDto;
import com.fzhucc.modules.notice.domain.dto.NoticeDto;
import com.fzhucc.modules.notice.domain.dto.NoticeEditDto;
import com.fzhucc.modules.notice.service.NoticeService;
import com.fzhucc.modules.notice.domain.dto.NoticeQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yyg
 * @date 2022-11-16
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "通知公告:管理")
@RequestMapping("/api/notice")
public class NoticeController {

    private final NoticeService noticeService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/show/download")
    @PreAuthorize("@el.check('notice:list')")
    public void exportNotice(HttpServletResponse response, NoticeQueryCriteria criteria) throws IOException {
        noticeService.download(noticeService.queryAll(criteria), response);
    }


    @Log("查询通知公告")
    @ApiOperation("查询通知公告")
    @AnonymousGetMapping("/show")
    public ResponseEntity<Object> queryNotice(NoticeQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(noticeService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增通知公告")
    @ApiOperation("新增通知公告")
    @PreAuthorize("@el.check('notice:add')")
    public ResponseEntity<Object> createNotice(@Validated @RequestBody NoticeAddDto resources){
        return new ResponseEntity<>(noticeService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改通知公告")
    @ApiOperation("修改通知公告")
    @PreAuthorize("@el.check('notice:edit')")
    public ResponseEntity<Object> updateNotice(@Validated @RequestBody NoticeEditDto resources){
        noticeService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除通知公告")
    @ApiOperation("删除通知公告")
    @PreAuthorize("@el.check('notice:del')")
    public ResponseEntity<Object> deleteNotice(@RequestBody Integer[] ids) {
        noticeService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("上传Banner图")
    @PostMapping(value = "/uploadPhoto")
    public ResponseEntity<Object> uploadPhoto(@RequestParam MultipartFile photo, @RequestParam Integer id){
        return new ResponseEntity<>(noticeService.douploadpic(photo,id), HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping("/{id}")
    @Log("查询单个通知公告")
    @ApiOperation("查询单个通知公告")
    public NoticeDto queryNoticeById(@PathVariable Integer id) {
        return noticeService.findById(id);
    }
}